The AppEvents module in the Scripting app provides an interface to observe global application state changes, such as scene lifecycle transitions and changes in system-wide appearance (light/dark mode). These capabilities are essential for writing responsive scripts that react appropriately to runtime context.
ScenePhaseRepresents the state of the app’s scene:
active – The app is in the foreground and interactive.inactive – The app is in transition or temporarily inactive.background – The app is running in the background and not visible.ColorSchemeReflects the current appearance mode of the device:
light – Light mode UIdark – Dark mode UIAppEventListenerManager<T>A generic event manager that allows you to register and unregister listeners dynamically. Used for both scenePhase and colorScheme.
AppEvents ClassAppEvents.scenePhaseListen for scene phase transitions. Ideal for responding to foreground/background state changes.
AppEvents.colorSchemeObserve system-wide light/dark appearance changes in real time.
useColorScheme() HookThis hook provides a reactive way to access the current ColorScheme ('light' or 'dark') within a component. It automatically updates the value when the system theme changes.
AppEvents should be manually removed when no longer needed to prevent memory leaks.useColorScheme() is the recommended way to reactively reflect the current theme in your components.